package state.component;


import state.Clip;
import state.GameGlobal;
import state.Graphics;
import state.Key;
import state.Manager;
import state.Sprite;
import state.font.ImageText;
import development.Constants;

public class BottomBar extends Component {
  private String leftLabel;
  
  private String centerLabel;
  
  private String rightLabel;
  
  private int choosedMenuIndex = -1;
  
  private Menu leftMenu;
  private Menu rightMenu;
  
  private Clip reuseClip = new Clip(0, 0, 0, 0);
  
  public BottomBar() {
    setSize(Constants.SCREEN_WIDTH, 25);
    setPosition(0, Constants.SCREEN_HEIGHT - 25);
  }
  
  public BottomBar(Manager manager, int height) {
    manager.setBottomBar(this);
    this.manager = manager;
    setSize(Constants.SCREEN_WIDTH, height);
    int realScreenHeight = GameGlobal.systemCanvas.getHeight();
    int screenHeight = Math.min(realScreenHeight, Constants.SCREEN_HEIGHT);
    setPosition(0, screenHeight - height);
  }

  public void pointerReleased(Sprite source, int x, int y) {
    if (manager == null) {
      System.out.println("Warning: need put manager to BottomBar to process action");
      return;
    }
    manager.stopEvent();
    
    switch (choosedMenuIndex) {
    case 0:
      Key.isCommandKey = true;
      manager.getParent().doKeyReleased(Key.SOFT_LEFT);
      break;
    case 1:
      Key.isCommandKey = true;
      manager.getParent().doKeyReleased(Key.FIRE);
      break;
    case 2:
      Key.isCommandKey = true;
      manager.getParent().doKeyReleased(Key.SOFT_RIGHT);
      break;
    }
    choosedMenuIndex = -1;
  }
  
  public void pointerPressed(Sprite source, int x, int y) {
    if (manager == null) {
      System.out.println("Warning: need put manager to BottomBar to process action");
      return;
    }
    manager.stopEvent();
    choosedMenuIndex = x / (Constants.SCREEN_WIDTH / 3);
    
    switch (choosedMenuIndex) {
    case 0:
      manager.getParent().doKeyPressed(Key.SOFT_LEFT);
      break;
    case 1:
      manager.getParent().doKeyPressed(Key.FIRE);
      break;
    case 2:
      manager.getParent().doKeyPressed(Key.SOFT_RIGHT);
      break;
    }
  }
  
  public void setManager(Manager manager) {
    manager.setBottomBar(this);
    this.manager = manager;
  }
  
  public void draw(Graphics g) {
    Clip.getClip(g, reuseClip);
    g.setClip(0, 0, Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT);
    
    if (isFillBackGround) {
      g.setColor(backgroundColor);
      g.fillRect(realX, realY, width, height);
      
      if (focusable) {
        if (choosedMenuIndex > -1) {
          g.setColor(focusBgColor);
          g.fillRect(realX + choosedMenuIndex * (width / 3), realY, width / 3, height);
        }
      }
    }

    if (hasBorder) {
      g.setColor(borderColor);
      g.drawRoundRect(realX, realY, width, height, 5, 5);
    }
    
    if (image != null) {
      g.drawImage(image, realX, realY, Constants.TOP_LEFT_ANCHOR);
    }
    
    ImageText font = getFont();
    
    // Draw label and menu in left side
    if (leftLabel != null) {
      String labelTmp = leftLabel;
      if ((leftMenu != null) && (leftMenu.isShowing())) {
        leftMenu.draw(g);
        labelTmp = "Đóng";
      }
      
      if (choosedMenuIndex == 0) {
        font.setBold(true);
        if (focusable) {
          font.drawString(g, labelTmp, focusFgColor, realX + 5, realY + height - 5, Constants.BOTTOM_LEFT_ANCHOR);
        } else {
          font.drawString(g, labelTmp, forceGroundColor, realX + 5, realY + height - 5, Constants.BOTTOM_LEFT_ANCHOR);
        }
        font.setBold(false);
      } else {
        font.drawString(g, labelTmp, forceGroundColor, realX + 5, realY + height - 5, Constants.BOTTOM_LEFT_ANCHOR);
      }
    }
    
    // Draw label center side
    if (centerLabel != null) {
      if (choosedMenuIndex == 1) {
        font.setBold(true);
        if (focusable) {
          font.drawString(g, centerLabel, focusFgColor, realX + width / 2, realY + height - 5, Constants.BOTTOM_HCENTER_ANCHOR);
        } else {
          font.drawString(g, centerLabel, forceGroundColor, realX + width / 2, realY + height - 5, Constants.BOTTOM_HCENTER_ANCHOR);
        }
        font.setBold(false);
      } else {
        font.drawString(g, centerLabel, forceGroundColor, realX + width / 2, realY + height - 5, Constants.BOTTOM_HCENTER_ANCHOR);
      }
    }
    
    // Draw label and menu in right side
    if (rightLabel != null) {
      String labelTmp = rightLabel;
      if ((rightMenu != null) && (rightMenu.isShowing())) {
        rightMenu.draw(g);
        labelTmp = "Đóng";
      }
      
      if (choosedMenuIndex == 2) {
        font.setBold(true);
        if (focusable) {
          font.drawString(g, labelTmp, focusFgColor, width - 5, realY + height - 5, Constants.BOTTOM_RIGHT_ANCHOR);
        } else {
          font.drawString(g, labelTmp, forceGroundColor, width - 5, realY + height - 5, Constants.BOTTOM_RIGHT_ANCHOR);
        }
        font.setBold(false);
      } else {
        font.drawString(g, labelTmp, forceGroundColor, width - 5, realY + height - 5, Constants.BOTTOM_RIGHT_ANCHOR);
      }
    }
    
    Clip.setClip(g, reuseClip);
  }
  
  public void reset() {
    leftLabel = null;
    centerLabel = null;
    rightLabel = null;
    leftMenu = null;
    rightMenu = null;
  }
  
  public void keyReleased(Sprite source, int keyCode) {
    // Kiểm tra hiển thị menu bên trái
    if ((keyCode == Key.SOFT_LEFT) && (leftMenu != null)) {
      if (leftMenu.isShowing()) {
        leftMenu.hide();
      } else {
        leftMenu.show();
      }
      return;
    }
    
    // Kiểm tra hiển thị menu bên phải
    if ((keyCode == Key.SOFT_RIGHT) && (rightMenu != null)) {
      if (rightMenu.isShowing()) {
        rightMenu.hide();
      } else {
        rightMenu.show();
      }
      return;
    }
  }
  
  public void setLeftMenu(String leftLabel, Menu menu) {
    this.leftLabel = leftLabel;
    this.leftMenu = menu;
    if (menu != null) {
      leftMenu.setPosition(leftMenu.getMenuX(), getRealY() - leftMenu.itemHeight);
      leftMenu.setBottomBar(this);
    }
  }
  
  public void setRightMenu(String rightLabel, Menu menu) {
    this.rightLabel = rightLabel;
    this.rightMenu = menu;
    if (menu != null) {
      rightMenu.setPosition(rightMenu.getMenuX(), getRealY() - rightMenu.itemHeight);
      rightMenu.setBottomBar(this);
    }
  }
  
  public Menu getLeftMenu() {
    return leftMenu;
  }

  public Menu getRightMenu() {
    return rightMenu;
  }
  
  public boolean isMenuShowing() {
    return (leftMenu != null && leftMenu.isShowing()) || (rightMenu != null && rightMenu.isShowing());
  }

  public void setLabels(String leftLabel, String centerLabel, String rightLabel) {
    setLeftLabel(leftLabel);
    setCenterLabel(centerLabel);
    setRightLabel(rightLabel);
  }

  public String getLeftLabel() {
    return leftLabel;
  }

  public void setLeftLabel(String leftLabel) {
    this.leftLabel = leftLabel;
  }

  public String getCenterLabel() {
    return centerLabel;
  }

  public void setCenterLabel(String centerLabel) {
    this.centerLabel = centerLabel;
  }

  public String getRightLabel() {
    return rightLabel;
  }

  public void setRightLabel(String rightLabel) {
    this.rightLabel = rightLabel;
  }
}
